package com.ssyan.appointment.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ssyan.appointment.hosp.feign.DictFeignClient;
import com.ssyan.appointment.hosp.repository.HospitalRepository;
import com.ssyan.appointment.hosp.service.HospitalService;
import com.ssyan.appointment.model.hosp.Hospital;
import com.ssyan.appointment.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    //注入远程调用封装接口
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {

        //把参数map集合转换为对象（Hospital）[JSONObject: Map<> - String{json} - Hospital]
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        //如果存在，进行修改
        if (hospitalExist != null){
            //设置基本状态不变，其余的设置保持request传入的信息，然后存入mongodb
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //第一次入库，设置未上线以及入库时间
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    //医院列表（条件查询分页）
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                                    .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                                    .withIgnoreCase(true);
        //将vo对象转换为Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        //调用方法实现查询
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //获取查询list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item->{
            this.setHospitalHosType(item);
        });

        return pages;
    }

    //更新医院的状态
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String,Object> map = new HashMap<>();
        //根据ID查询医院信息,传入医院信息封装方法中，获取到医院基本信息和封装的添加信息
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        map.put("hospital",hospital);
        //抽取预约信息单独封装
        map.put("bookingRule",hospital.getBookingRule());
        //不需要重复返回同样的信息，再设为ks
        hospital.setBookingRule(null);
        return map;
    }

    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype()).getData().toString();

        //查询省 市 地区
        String proviceString = dictFeignClient.getName(hospital.getProvinceCode()).getData().toString();
        String cityString = dictFeignClient.getName(hospital.getCityCode()).getData().toString();
        String districtString = dictFeignClient.getName(hospital.getDistrictCode()).getData().toString();

        //将获取的医院等级封装到新自定义的字段中
        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",proviceString+cityString+districtString);
        return hospital;
    }
}
